92334 - 신고 결과 받기
정보
- 문제 보기: 92334 - 신고 결과 받기
- 소요 시간: 22분 25초
- 풀이 언어:
java - 체감 난이도: 2️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
해시 자료구조
풀이 코드
정보
- 메모리: 172000 KB
- 시간: 143 ms
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
Map<String, Set<String>> reporterMap = new HashMap<>();
Map<String, Integer> mailCntMap = new HashMap<>();
// init
for (String id : id_list) {
reporterMap.put(id, new HashSet<>());
mailCntMap.put(id, 0);
}
// collect report
for (String r : report) {
String[] rArr = r.split(" ");
String from = rArr[0];
String to = rArr[1];
Set<String> fromSet = reporterMap.get(to); // shallow copy
if (fromSet.contains(from)) continue; // 'from' should be unique
fromSet.add(from);
}
// send mail
for (Map.Entry<String, Set<String>> e : reporterMap.entrySet()) {
Set<String> fromSet = e.getValue();
int reportCnt = fromSet.size();
if (reportCnt < k) continue;
for (String from : fromSet) {
mailCntMap.merge(from, 1, Integer::sum);
}
}
// count
int[] ans = new int[id_list.length];
for (int i = 0; i < id_list.length; ++i) {
ans[i] = mailCntMap.get(id_list[i]);
}
return ans;
}
}